import {
  getChannelList,
  addChannel,
  editChannel,
  deleteChannel,
  detailChannel,
  startStatus,
  getChannelType,
} from "../server/channelApi";

const initData = {
  channelList: [],
  totalCount: 0,
};

export default {
  namespace: "channel",
  state: {
    ...initData,
  },

  effects: {
    *getChannelList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas, totalCount } = yield call(
        getChannelList,
        payload,
      );
      yield put({ type: "get/channelList", payload: datas, totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *addChannel({ payload }, { call }) {
      const resp = yield call(addChannel, payload);
      return resp;
    },
    *editChannel({ payload }, { call }) {
      const resp = yield call(editChannel, payload);
      return resp;
    },
    *deleteChannel({ payload }, { call }) {
      const resp = yield call(deleteChannel, payload);
      return resp;
    },
    *detailChannel({ payload }, { call }) {
      const resp = yield call(detailChannel, payload);
      return resp;
    },
    *startStatus({ payload }, { call }) {
      const resp = yield call(startStatus, payload);
      return resp;
    },
    *getChannelType({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(
        getChannelType,
        payload,
      );
      return { errCode, errMsg: errMsg || errortext, datas };
    },
  },
  reducers: {
    "get/channelList": (state, { payload, totalCount }) => ({
      ...state,
      channelList: payload,
      totalCount,
    }),
    clearData: () => ({ ...initData }),
  },
};
